package com.gec.obwiki.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
@Slf4j   //lombok 日志注解
public class MybatisPlusConfig {

    @Bean
    public MetaObjectHandler getMetaObjectHandler(){
        return  new MetaObjectHandler(){
            @Override
            public void insertFill(MetaObject metaObject) {
                //lombok生成的 日志对象  可以查看编译后代码得到
                log.info("start insert fill ....");
                this.setFieldValByName("createTime", new Date(), metaObject);
                this.setFieldValByName("updateTime", new Date(), metaObject);
            }
            @Override
            public void updateFill(MetaObject metaObject) {
                log.info("start update fill ....");
                this.setFieldValByName("updateTime", new Date(), metaObject);
            }
        };
    }
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        //分页插件
        //新的分页插件,一缓和二缓遵循mybatis的规则
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}